package main

import "html/template"
import "os"

type Friend struct {
	Name string
}

type Person struct {
	UserName string
	Emails   []string
	Friends  []*Friend
}

func main() {
	t := template.New("fieldname example")
	t, _ = t.Parse(`hello {{.UserName}}!
		{{range .Emails}}
			and email{{.}}
		{{end}}
		{{with .Friends}}
		{{range .}}
			my friend name is {{$i}} {{.Name}}
		{{end}}
		{{end}}
		`)
	p := Person{}
	p.UserName = "Alpha2"
	p.Emails = []string{"Alpha2@126.com", "Alpha2@qq.com"}
	f1 := Friend{Name: "张三"}
	f2 := Friend{Name: "李四"}
	p.Friends = []*Friend{&f1, &f2}

	t.Execute(os.Stdout, p)
}
